package com.jdone.compus.service;

import com.jdone.compus.model.GiftSubmission;
import com.jdone.compus.repository.GiftSubmissionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;

@Service
public class GiftSubmissionService {

    @Autowired
    private GiftSubmissionRepository repository;

    public GiftSubmission createGiftSubmission(GiftSubmission giftSubmission) {
        return repository.save(giftSubmission);
    }

    public List<GiftSubmission> getAllGiftSubmissions() {
        return repository.findAll();
    }

    public Optional<GiftSubmission> getGiftSubmissionById(Long id) {
        return repository.findById(id);
    }

    public GiftSubmission updateGiftSubmission(Long id, GiftSubmission updatedGiftSubmission) {
        return repository.findById(id).map(existingGiftSubmission -> {
            existingGiftSubmission.setStudentId(updatedGiftSubmission.getStudentId());
            existingGiftSubmission.setSubmissionTime(updatedGiftSubmission.getSubmissionTime());
            existingGiftSubmission.setGiftName(updatedGiftSubmission.getGiftName());
            existingGiftSubmission.setGiftType(updatedGiftSubmission.getGiftType());
            existingGiftSubmission.setStatus(updatedGiftSubmission.getStatus());
            return repository.save(existingGiftSubmission);
        }).orElseThrow(() -> new RuntimeException("Gift submission not found"));
    }

    public void deleteGiftSubmission(Long id) {
        repository.deleteById(id);
    }
}
